﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ircclient
{
    public partial class FormChat : Form
    {
        protected HtmlDocument ch;
        protected HtmlElement el;
        protected int numMessage;

        public FormChat()
        {
            InitializeComponent();
            Size len = TextRenderer.MeasureText("|", commandBox.Font);
            commandBox.Height = len.Height + 6;

        }
        public void OpenUserBox()
        {
            if (splitContainer1.InvokeRequired)
            {
                splitContainer1.Invoke(new Action(() => { splitContainer1.Panel2Collapsed = false; }));
            }
            else
                splitContainer1.Panel2Collapsed = false;
        }
        public void AddUserBox(string[] users, bool clear = false)
        {
            if (clear == false)
            {
                if (userBox.InvokeRequired)
                {
                    userBox.Invoke(new Action(() => { userBox.Items.Clear(); }));

                }
                else
                    userBox.Items.Clear();
            }
            if (userBox.InvokeRequired)
            {
                userBox.Invoke(new Action(() => {userBox.Items.AddRange(users);}));
            }
            else
                userBox.Items.AddRange(users);
        }
        public void AddUserBox(string user)
        {
            if (userBox.InvokeRequired)
            {
                userBox.Invoke(new Action(() => { userBox.Items.Add(user); }));
            }
            else
                userBox.Items.Add(user);
        }
        public void DelUserBox(string user)
        {
            if (userBox.InvokeRequired)
            {
                userBox.Invoke(new Action(() => { userBox.Items.Remove(user); }));
            }
            else
                userBox.Items.Remove(user);
        }

        public void View()
        {
            
            if (webChat.InvokeRequired)
            {
                webChat.Invoke(new Action(() =>
                {
                    this.Focus();
                    el.ScrollIntoView(false);
                }));

            }
            else
            {
               
                this.Focus();
                el.ScrollIntoView(false);
            }

            this.Focus();
        }

        public void SetTopicBox(string text)
        {
            
            if( TopicBox.InvokeRequired)
            {
                TopicBox.Invoke(new Action<string>((s) => TopicBox.Text = s), text);

            }
            else
                TopicBox.Text = text;
        }


        private void FormChat_Load(object sender, EventArgs e)
        {
            //webChat.DocumentText = "";
            webChat.Navigate("file://D:/blank.html");
            while (webChat.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents();
            ch = webChat.Document;
            //ch.Write("<html><head><script src=\"file://d:/script.js\"></script><link rel=\"stylesheet\" type=\"text/css\" href=\"file://d:/style.css\" /></head><body><div id=main></div></body></html>");
            el = ch.All["main"];
            
        }
        public void AddTextToChat(string text)
        {

            if (webChat.InvokeRequired)
            {
                webChat.Invoke(new Action(() =>
                {
                    el.InnerHtml += "<div id=\"mes\">" + text + "</div>";
                    numMessage++;
                    el.ScrollIntoView(false);
                    if (numMessage > 50)
                    {
                        webChat.Document.InvokeScript("del_element", new String[] { "#mes" });
                    }
                }));

            }
            else
            {
                el.InnerHtml += "<div id=\"mes\">" + text + "</div>";
                numMessage++;
                el.ScrollIntoView(false);
                if (numMessage > 50)
                {
                    webChat.Document.InvokeScript("del_element", new String[] { "#mes" });
                }
            }

        }

        public class CustomEventArgs : EventArgs
        {
            public CustomEventArgs(string s)
            {
                message = s;
            }
            private string message;

            public string Message
            {
                get { return message; }
                set { message = value; }
            }
        }

        public event EventHandler<FormChat.CustomEventArgs> RaiseCustomEvent;

        protected virtual void SendChanEvent(FormChat.CustomEventArgs e)
        {
            EventHandler<FormChat.CustomEventArgs> handler = RaiseCustomEvent;

            if (handler != null)
            {

                handler(this, e);
            }
        }

        private void commandBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendChanEvent(new FormChat.CustomEventArgs(commandBox.Text));
                commandBox.Text = "";
            }
        }
        

    }
}
